//
//  YKSelectDrugViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/4.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKSelectDrugViewController.h"
#import "YKClientListHeaderView.h"
#import "YKSelectDrugItem.h"
#import "YKPopupView.h"
#import "TFHttpManager.h"

@interface YKSelectDrugViewController ()<UISearchBarDelegate>
@property (nonatomic, strong) NSArray *models;
@end

@implementation YKSelectDrugViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.placeholder = @"通用名/助记码/生产厂家";
    searchBar.delegate = self;
    [self.view addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    
    YKClientListHeaderView *listHeaderView = [[YKClientListHeaderView alloc]initWithTitles:@[@"选择",@"药品信息",@"类型",@"协议价"] weights:@[@1,@3,@1,@1] spaceing:0 leading:3 trailing:3];
    listHeaderView.textFont = [UIFont systemFontOfSize:16];
    
    [self.view addSubview:listHeaderView];
    [listHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(searchBar.mas_bottom);
    }];
    
    UIButton *confirmButton = [[UIButton alloc]init];
    [confirmButton setTitle:@"确认添加" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [confirmButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [self.view addSubview:confirmButton];
    [confirmButton addTarget:self action:@selector(onConfirmButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    confirmButton.layer.cornerRadius = 5;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(listHeaderView.mas_bottom);
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(confirmButton.mas_top);
    }];
    
    [self tf_showStateView:kTFViewStateLoading inView:self.tableView];
    [TFHTTPMANAER POST:@"customerDrugPriceDetail/queryProductInfo.htm"
            parameters:@{@"name":@""}
         cacheDuration:60 *60 *24 *30
               success:^(id responseObject) {
        self.models = [NSArray yy_modelArrayWithClass:[YKSelectDrugModel class] json:responseObject[@"productList"]];
        [self updateViews];
        [self tf_showStateView:kTFViewStateNone inView:self.tableView];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError inView:self.tableView];
    }];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    NSString *name = searchBar.text;
    [self tf_showStateView:kTFViewStateLoading inView:self.tableView];
    [TFHTTPMANAER POST:@"customerDrugPriceDetail/queryProductInfo.htm" parameters:@{@"name":name} success:^(id responseObject) {
        self.models = [NSArray yy_modelArrayWithClass:[YKSelectDrugModel class] json:responseObject[@"productList"]];
        [self updateViews];
        [self tf_showStateView:kTFViewStateNone inView:self.tableView];
    } failure:^(id responseObject, NSError *error) {
        [self tf_showStateView:kTFViewStateNetError inView:self.tableView];
    }];
}

- (void)updateViews {
    [self.tableViewManager removeAllSections];
    TFTableViewSection *section = [TFTableViewSection section];
    section.headerHeight = .1f;
    section.footerHeight = .1f;
    for (YKSelectDrugModel *model in self.models) {
        if ([self.selectedDrugs containsObject:model]) {
            continue;
        }
        model.DRUGNAME = model.CINV_NAME;
        model.DRUGPRICE = model.WHOLE_PRICE;
        YKSelectDrugItem *item = [YKSelectDrugItem itemWithModel:model];
        //item.cellHeight = 95;
        item.registerCellClassName = @"YKClientDrugPriceDetailDetailItemCell";
        [section addItem:item];
    }
    [self.tableViewManager addSection:section];
    
    [self.tableView reloadData];
}

- (void)onConfirmButtonTapped:(id)sender {
    NSArray *selectedDrugs = [[self.tableViewManager.sections.firstObject.items tf_filteredWithPredicateBlock:^BOOL(YKSelectDrugItem*  _Nonnull obj, NSInteger idx) {
        return obj.selected;
    }]tf_mapUsingBlock:^id _Nullable(YKSelectDrugItem*  _Nonnull obj, NSInteger idx) {
        return obj.model;
    }];
    for (YKSelectDrugModel *model in selectedDrugs) {
        if (model.DRUGPRICE.length == 0) {
            [self showHint:@"请填入协议价"];
            return;
        }
//        if (model.drugprice.doubleValue < model.LOWEST_PRICE.doubleValue) {
//            [self showHint:@"协议价不能低于底限价"];
//            return;
//        }
//        if (model.drugprice.doubleValue > model.WHOLE_PRICE.doubleValue) {
//            [self showHint:@"协议价不能大于基准批发价"];
//            return;
//        }
    }
    if (self.selectDrugComplete) {
        self.selectDrugComplete(selectedDrugs);
    }
    
    [YKPopupView dismissViewController:self];
}
@end
